package com.qinghong.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qinghong.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/payment/hystrix/ok/{id}")
    public String paymnentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentService.paymnentInfo_OK(id);
        log.info(result);
        return result;
    }

    @GetMapping(value = "/payment/hystrix/timeout/{id}")
//    public String paymnentInfo_TimeOut(@PathVariable("id") Integer id){
//        String result = paymentService.paymnentInfo_TimeOut(id);
//        log.info(result);
//        return result;
//    }
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String paymnentInfo_TimeOut(@PathVariable("id") Integer id){
        String s = paymentService.paymnentInfo_TimeOut(id);
        return s;
    }

    public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id){
        return "消费者80"+Thread.currentThread().getName()+"出错";
    }

    @GetMapping(value = "/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String s = paymentService.paymentCircuitBreaker(id);
        return s;
    }
}

